/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MonedaView2.java
 *
 * Created on 08/11/2009, 09:59:38 PM
 */

package solucionessgh.Moneda;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import solucionessgh.Util.Reporte;

/**
 *
 * @author roderick
 */
public class MonedaView extends javax.swing.JDialog {

    private MonedaModel model;
    private int id;
    private int action;//1=nuevo,2=modificar o eliminar

    public MonedaView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();        
        model = new MonedaModel();
        setResizable(false);
        txt_mondesc.setEditable(false);
        txt_monsim.setEditable(false);

        btn_nuevo.setEnabled(true);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(false);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(false);

        java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
        java.awt.Dimension screenSize = tk.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setLocation(screenWidth / 4, screenHeight / 4);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txt_mondesc = new javax.swing.JTextField();
        txt_monsim = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btn_buscar = new javax.swing.JButton();
        btn_print = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        cb_monpred = new javax.swing.JCheckBox();
        cb_monact = new javax.swing.JCheckBox();
        btn_nuevo = new javax.swing.JButton();
        btn_editar = new javax.swing.JButton();
        btn_grabar = new javax.swing.JButton();
        btn_eliminar = new javax.swing.JButton();
        btn_cancelar = new javax.swing.JButton();
        btn_salir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Moneda");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Moneda"));

        txt_mondesc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_mondescActionPerformed(evt);
            }
        });
        txt_mondesc.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txt_mondescFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txt_mondescFocusLost(evt);
            }
        });

        txt_monsim.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txt_monsimFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txt_monsimFocusLost(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Descripcion : ");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Simbolo : ");

        btn_buscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/find.png"))); // NOI18N
        btn_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_buscarActionPerformed(evt);
            }
        });

        btn_print.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/print.png"))); // NOI18N
        btn_print.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_printActionPerformed(evt);
            }
        });

        cb_monpred.setFont(new java.awt.Font("Tahoma", 1, 11));
        cb_monpred.setText("Predeterminada");

        cb_monact.setFont(new java.awt.Font("Tahoma", 1, 11));
        cb_monact.setText("Activa");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btn_buscar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btn_print))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txt_monsim, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txt_mondesc, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cb_monpred)
                        .addGap(45, 45, 45)
                        .addComponent(cb_monact)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btn_buscar)
                    .addComponent(btn_print))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_mondesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_monsim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cb_monpred)
                    .addComponent(cb_monact))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btn_nuevo.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_nuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/new.png"))); // NOI18N
        btn_nuevo.setText("Nuevo");
        btn_nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_nuevoActionPerformed(evt);
            }
        });

        btn_editar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_editar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/edit.png"))); // NOI18N
        btn_editar.setText("Editar");
        btn_editar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_editarActionPerformed(evt);
            }
        });

        btn_grabar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_grabar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/save.png"))); // NOI18N
        btn_grabar.setText("Grabar");
        btn_grabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_grabarActionPerformed(evt);
            }
        });

        btn_eliminar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_eliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/delete.png"))); // NOI18N
        btn_eliminar.setText("Eliminar");

        btn_cancelar.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/close.png"))); // NOI18N
        btn_cancelar.setText("Cancelar");
        btn_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelarActionPerformed(evt);
            }
        });

        btn_salir.setFont(new java.awt.Font("Tahoma", 0, 12));
        btn_salir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/solucionessgh/resources/exit.png"))); // NOI18N
        btn_salir.setText("Salir");
        btn_salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_salirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btn_cancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_eliminar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_grabar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_editar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btn_nuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(btn_salir, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn_nuevo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_editar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_grabar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_eliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_cancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_salir))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txt_mondescFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txt_mondescFocusGained
        txt_mondesc.setBackground(new java.awt.Color(255, 255, 204));
    }//GEN-LAST:event_txt_mondescFocusGained

    private void txt_mondescFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txt_mondescFocusLost
        txt_mondesc.setBackground(new java.awt.Color(255, 255, 255));
    }//GEN-LAST:event_txt_mondescFocusLost

    private void txt_monsimFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txt_monsimFocusGained
        txt_monsim.setBackground(new java.awt.Color(255, 255, 204));
    }//GEN-LAST:event_txt_monsimFocusGained

    private void txt_monsimFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txt_monsimFocusLost
        txt_monsim.setBackground(new java.awt.Color(255, 255, 255));
    }//GEN-LAST:event_txt_monsimFocusLost

    private void btn_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_buscarActionPerformed
        BuscarView dialog = new BuscarView(new javax.swing.JFrame(), true, model);
        dialog.setVisible(true);
        if(dialog.getId()==0)
            return;

        ResultSet rs = this.model.finder(dialog.getId());
        try {
            if(rs.next()){
                id = rs.getInt("moncod");
                txt_mondesc.setText(rs.getString("mondesc"));
                txt_monsim.setText(rs.getString("monsim"));
                cb_monpred.setSelected(rs.getBoolean("monpred"));
                cb_monact.setSelected(rs.getBoolean("monact"));
            }
        }catch (SQLException ex) {
                System.err.println(ex.toString());
        }

        btn_nuevo.setEnabled(false);
        btn_editar.setEnabled(true);
        btn_grabar.setEnabled(false);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(false);

    }//GEN-LAST:event_btn_buscarActionPerformed

    private void btn_nuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_nuevoActionPerformed
        txt_mondesc.setEditable(true);
        txt_monsim.setEditable(true);
        txt_mondesc.requestFocusInWindow();

        btn_nuevo.setEnabled(false);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(true);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(true);

        id=0;
        action=1;
    }//GEN-LAST:event_btn_nuevoActionPerformed

    private void btn_grabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_grabarActionPerformed
        model.setMondesc(txt_mondesc.getText());
        model.setMonsim(txt_monsim.getText());
        model.setMonpred(cb_monpred.isSelected());
        model.setMonact(cb_monact.isSelected());
        model.setId(id);

        if(this.action==1){//grabar
            if(model.create()){
                JOptionPane.showMessageDialog(null,
                    "Grabacion OK",
                    "Moneda",
                    JOptionPane.INFORMATION_MESSAGE);
                txt_mondesc.setText("");
                txt_monsim.setText("");
                cb_monpred.setSelected(false);
                cb_monact.setSelected(false);

                btn_nuevo.setEnabled(true);
                btn_editar.setEnabled(false);
                btn_grabar.setEnabled(false);
                btn_eliminar.setEnabled(false);
                btn_cancelar.setEnabled(false);
                return;
            }
        }
                if(this.action==2){//modificar
            int n = JOptionPane.showConfirmDialog(
                null,
                "Desea Mofiicar el campo "+this.txt_mondesc.getText(),
                "Modificar",
                JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                if(model.update()){
                    if (n == JOptionPane.YES_OPTION) {
                        JOptionPane.showMessageDialog(null,
                            "Modificacion OK",
                            "Moneda",
                            JOptionPane.INFORMATION_MESSAGE);
                        txt_mondesc.setText("");
                        txt_monsim.setText("");
                        cb_monpred.setSelected(false);
                        cb_monact.setSelected(false);

                        btn_nuevo.setEnabled(true);
                        btn_editar.setEnabled(false);
                        btn_grabar.setEnabled(false);
                        btn_eliminar.setEnabled(false);
                        btn_cancelar.setEnabled(false);
                        return;
                    }
                }
            }
        }

    }//GEN-LAST:event_btn_grabarActionPerformed

    private void btn_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelarActionPerformed
        txt_mondesc.setText("");
        txt_monsim.setText("");
        cb_monpred.setSelected(false);
        cb_monact.setSelected(false);

        btn_nuevo.setEnabled(true);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(false);
        btn_eliminar.setEnabled(false);
        btn_cancelar.setEnabled(false);
    }//GEN-LAST:event_btn_cancelarActionPerformed

    private void btn_editarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_editarActionPerformed
        action=2;

        btn_nuevo.setEnabled(false);
        btn_editar.setEnabled(false);
        btn_grabar.setEnabled(true);
        btn_eliminar.setEnabled(true);
        btn_cancelar.setEnabled(true);

        txt_mondesc.setEditable(true);
        txt_monsim.setEditable(true);
        txt_mondesc.requestFocusInWindow();
    }//GEN-LAST:event_btn_editarActionPerformed

    private void btn_salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_salirActionPerformed
        setVisible(true);
        dispose();
    }//GEN-LAST:event_btn_salirActionPerformed

    private void btn_printActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_printActionPerformed
        Reporte rep = new Reporte("/solucionessgh.Moneda/EstadoMoneda.jasper");
        try {
            rep.setVisible();
        } catch (ClassNotFoundException ex) {
            System.out.println(ex.toString());
        }
    }//GEN-LAST:event_btn_printActionPerformed

    private void txt_mondescActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_mondescActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_mondescActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_buscar;
    private javax.swing.JButton btn_cancelar;
    private javax.swing.JButton btn_editar;
    private javax.swing.JButton btn_eliminar;
    private javax.swing.JButton btn_grabar;
    private javax.swing.JButton btn_nuevo;
    private javax.swing.JButton btn_print;
    private javax.swing.JButton btn_salir;
    private javax.swing.JCheckBox cb_monact;
    private javax.swing.JCheckBox cb_monpred;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField txt_mondesc;
    private javax.swing.JTextField txt_monsim;
    // End of variables declaration//GEN-END:variables

}
